// 
// Bachelor of Software Engineering
// Media Design School
// Auckland
// New Zealand
//
// (c) 2005 - 2014 Media Design School
//
// File Name	: HighscoreScreenView.cpp
// Desrciption	: CHighscoreScreenView implementation file.
// Author		: Hayden Asplet.
// Mail			: hayden.asplet@mediadesignschool.com
//

// PCH
#include "SpaceInvadersStd.h"

// Library Includes

// This Include
#include "HighscoreScreenView.h"

// Local Includes
#include "Msvc\resource.h"

const int CHighscoreScreenView::s_kiMAX_SCORES_TO_SHOW = 10;
const int CHighscoreScreenView::s_kiNAME_SIZE = 3;
const int CHighscoreScreenView::s_kiVERTICAL_OFFSET = 40;
const int CHighscoreScreenView::s_kiNAME_POS_X = 350;
const int CHighscoreScreenView::s_kiSCORE_POS_X = 500;
const int CHighscoreScreenView::s_kiPOS_Y = 150;
const int CHighscoreScreenView::s_kiCHAR_WIDTH = 17;
const float CHighscoreScreenView::s_kfFLASH_TIME = 0.5f;

CHighscoreScreenView::CHighscoreScreenView(std::vector<CHighScoreEntry>* _pHighscores)
	:m_pHighscores(_pHighscores),
	m_bIsFlash(false),
	m_fFlashTimer(0.0f)
{
	// Load the background from the resource.
	m_background = g_pApp->LoadSprite(IDB_HIGHSCORESCREEN);
}

CHighscoreScreenView::~CHighscoreScreenView()
{
	m_pHighscores = 0;
}

void CHighscoreScreenView::VUpdate(float _fDeltaTime)
{
	// Increment flash timer.
	m_fFlashTimer += _fDeltaTime;

	if(m_fFlashTimer > s_kfFLASH_TIME) {
	// Time to flash?
		// Reset the timer.
		m_fFlashTimer = 0.0f;
		
		// Toggle flash.
		m_bIsFlash = !m_bIsFlash;
	}
}

void CHighscoreScreenView::VRender(float _fDeltaTime)
{
	g_pApp->RenderSprite(m_background, 0, 0);

	for(unsigned int i = 0; i < m_pHighscores->size() && static_cast<int>(i) < s_kiMAX_SCORES_TO_SHOW; ++i) {
		std::string strTextOut;

		if((*m_pHighscores)[i].GetName() == "") {
		// A name needs to go here.
			g_pApp->RenderText("ENTER YOUR NAME: ", 40, s_kiPOS_Y + (i * s_kiVERTICAL_OFFSET), g_kGREEN, g_kiSTANDARD_TEXT_HEIGHT, "HaxrCorp S8");
			g_pApp->RenderText(m_strPlayerName, s_kiNAME_POS_X, s_kiPOS_Y + (i * s_kiVERTICAL_OFFSET), g_kWHITE, g_kiSTANDARD_TEXT_HEIGHT, "HaxrCorp S8");

			if(!m_bIsFlash && m_strPlayerName.size() < s_kiNAME_SIZE) {
			// The player needs to enter their name.
				// Show an underscore to draw the users attention.
				g_pApp->RenderText("_",															// Text.
								   s_kiNAME_POS_X + (s_kiCHAR_WIDTH * m_strPlayerName.size()),	// X position.
								   s_kiPOS_Y + (i * s_kiVERTICAL_OFFSET),						// Y position.
								   g_kWHITE,													// Colour.
								   g_kiSTANDARD_TEXT_HEIGHT, "HaxrCorp S8");					// Font settings.
			}
		}

		// Get the name of the current highscore entry.
		strTextOut = (*m_pHighscores)[i].GetName();
		
		// Render the name.
		g_pApp->RenderText(strTextOut, s_kiNAME_POS_X, s_kiPOS_Y + (i * s_kiVERTICAL_OFFSET), g_kGREEN, g_kiSTANDARD_TEXT_HEIGHT, "HaxrCorp S8");

		// Get the score of the current highscore entry as text.
		strTextOut = std::to_string(static_cast<long long>((*m_pHighscores)[i].GetScore()));

		// Render the score as text.
		g_pApp->RenderText(strTextOut, s_kiSCORE_POS_X, s_kiPOS_Y + (i * s_kiVERTICAL_OFFSET), g_kGREEN, g_kiSTANDARD_TEXT_HEIGHT, "HaxrCorp S8");
	}

	// Render game objects, if there is any.
	CRenderer* pRenderer = g_pApp->m_pGame->GetRenderer();

	if(pRenderer) {
		pRenderer->RenderGameObjects();
	}
}

bool CHighscoreScreenView::VMsgProc(const CAppMsg& _krMsg)
{
	switch(_krMsg.m_uMsg) {
		case WM_KEYDOWN: {
			if(_krMsg.m_wParam == VK_RETURN || _krMsg.m_wParam == VK_SPACE) {
				for(unsigned int i = 0; i <  m_pHighscores->size() && static_cast<int>(i) < s_kiMAX_SCORES_TO_SHOW; ++i) {
					if((*m_pHighscores)[i].GetName() == "") {
						if(m_strPlayerName.size() < s_kiNAME_SIZE) {
							return true;
						}
						else {
							// Set the name in the highscores.
							(*m_pHighscores)[i].SetName(m_strPlayerName);

							// Delete lower high scores.
							while(m_pHighscores->size() > s_kiMAX_SCORES_TO_SHOW) {
								m_pHighscores->pop_back();
							}

							return true;
						}

					}
				}

				g_pApp->m_pGame->VChangeState(GS_MainMenu);
			}
			else if(_krMsg.m_wParam == VK_BACK) {
				if(!m_strPlayerName.empty()) {
					m_strPlayerName.pop_back();
				}
			}
			for(int i = 'A'; i <= 'Z'; ++i) {
				if(_krMsg.m_wParam == i && m_strPlayerName.size() < s_kiNAME_SIZE) {
					m_strPlayerName += _krMsg.m_wParam;
				}
			}

			break;
		}
		case WM_KEYUP: {
			break;
		}
		case WM_MOUSEMOVE: {

			break;
		}
		case WM_LBUTTONDOWN: {

			break;
		}
		case WM_LBUTTONUP: {

			break;
		}
		case WM_RBUTTONDOWN: {

			break;
		}
		case WM_RBUTTONUP: {

			break;
		}
		default: break;
	}

	return true;
}
